package com.nativetalk.service.three.study.update;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.course.TdNtCoursePictureList;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.TdNtMemberDetail;
import com.nativetalk.bean.study.TdNtStudyTeachUpdate;
import com.nativetalk.bean.teacher.TdNtTeacherIndex;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtMemberDao;
import com.nativetalk.dao.TdNtStudyTeachDao;
import com.nativetalk.dao.TdNtTeacherDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/7/1.
 */
@Service
public class ThreeStudyUpdateServiceImpl implements  ThreeStudyUpdateService{
    private static Logger logger = Logger.getLogger(ThreeStudyUpdateServiceImpl.class);
    @Resource
    private TdNtStudyTeachDao tdNtStudyTeachDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Override
    public RetInfo cancelOrders(int stt_id,TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":cancelOrders:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtStudyTeachUpdate tdNtStudyTeachUpdate = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(stt_id);
            if (tdNtStudyTeachUpdate != null && tdNtStudyTeachUpdate.getStudy_state().equals("0")) {
                    tdNtStudyTeachUpdate.setStudy_state("6");
                    tdNtStudyTeachUpdate.setEnd_time(new Timestamp(System.currentTimeMillis()));
                    tdNtStudyTeachDao.updateTdNtStudyTeachCancel(tdNtStudyTeachUpdate);
                    retInfo.setMark("0");
                    retInfo.setTip("取消成功");

            } else {
                    retInfo.setMark("1");
                    retInfo.setTip("已经接单");
                    Map<String, Object> result = new HashMap<>();
                    result.put("memb_name",tdNtMember.getMemb_name());
                    result.put("memb_head_portrait",tdNtMember.getMemb_head_portrait());
                    result.put("stt_id", stt_id);
                    TdNtTeacherIndex tdNtTeacherIndex =  tdNtTeacherDao.findTdNtTeacherIndex(tdNtStudyTeachUpdate.getTea_id());
                    result.put("tea_name", tdNtTeacherIndex.getTea_name());
                    result.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = new ArrayList<>();
                    if ("100".equals(tdNtStudyTeachUpdate.getCou_id().toString())){

                        tdNtCoursePictureLists.add(0,(TdNtCoursePictureList)MemcachedUtils.get(MemcachedKey.freeCoursePicture));
                    }else{

                        Map<String , List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                        tdNtCoursePictureLists = tdNtCoursePictureListMap.get("" + tdNtStudyTeachUpdate.getCou_id());
                    }

                    result.put("course_pictures", tdNtCoursePictureLists);
                    result.put("channel",tdNtTeacherIndex.getTea_invite_code());
                    retInfo.setObj(result);
                }


        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo cancelTeacherOrders(int stt_id, TdNtTeacherResult tdNtTeacherResult) {
        String logInfo = this.getClass().getName() + ":cancelTeacherOrders:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtStudyTeachUpdate tdNtStudyTeachUpdate = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(stt_id);
            if (tdNtStudyTeachUpdate != null && tdNtStudyTeachUpdate.getStudy_state().equals("0")) {
                tdNtStudyTeachUpdate.setStudy_state("6");
                tdNtStudyTeachUpdate.setEnd_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.updateTdNtStudyTeachCancel(tdNtStudyTeachUpdate);
                retInfo.setMark("0");
                retInfo.setTip("取消成功");

            } else {
                retInfo.setMark("1");
                retInfo.setTip("已经接单");
                Map<String, Object> result = new HashMap<>();
                TdNtMemberDetail tdNtMember = tdNtMemberDao.findTdNtMemberDetail(tdNtStudyTeachUpdate.getMemb_id());
                result.put("memb_name",tdNtMember.getMemb_name());
                result.put("memb_head_portrait",tdNtMember.getMemb_head_portrait());
                result.put("stt_id", stt_id);
                result.put("tea_name", tdNtTeacherResult.getTea_name());
                result.put("tea_head_portrait", tdNtTeacherResult.getTea_head_portrait());
                List<TdNtCoursePictureList> tdNtCoursePictureLists = new ArrayList<>();
                if ("100".equals(tdNtStudyTeachUpdate.getCou_id().toString())){

                    tdNtCoursePictureLists.add(0,(TdNtCoursePictureList)MemcachedUtils.get(MemcachedKey.freeCoursePicture));
                }else{

                    Map<String , List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                    tdNtCoursePictureLists = tdNtCoursePictureListMap.get("" + tdNtStudyTeachUpdate.getCou_id());
                }

                result.put("course_pictures", tdNtCoursePictureLists);
                result.put("channel",tdNtTeacherResult.getTea_invite_code());
                retInfo.setObj(result);
            }


        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
